/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.job.service.impl;

import me.zhengjie.modules.job.domain.JobTypeChild;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.job.repository.JobTypeChildRepository;
import me.zhengjie.modules.job.service.JobTypeChildService;
import me.zhengjie.modules.job.service.dto.JobTypeChildDto;
import me.zhengjie.modules.job.service.dto.JobTypeChildQueryCriteria;
import me.zhengjie.modules.job.service.mapstruct.JobTypeChildMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author 毛晋辉
* @date 2020-09-02
**/
@Service
@RequiredArgsConstructor
public class JobTypeChildServiceImpl implements JobTypeChildService {

    private final JobTypeChildRepository jobTypeChildRepository;
    private final JobTypeChildMapper jobTypeChildMapper;

    @Override
    public Map<String,Object> queryAll(JobTypeChildQueryCriteria criteria, Pageable pageable){
        Page<JobTypeChild> page = jobTypeChildRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(jobTypeChildMapper::toDto));
    }

    @Override
    public List<JobTypeChildDto> queryAll(JobTypeChildQueryCriteria criteria){
        return jobTypeChildMapper.toDto(jobTypeChildRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public JobTypeChildDto findById(Integer id) {
        JobTypeChild jobTypeChild = jobTypeChildRepository.findById(id).orElseGet(JobTypeChild::new);
        ValidationUtil.isNull(jobTypeChild.getId(),"JobTypeChild","id",id);
        return jobTypeChildMapper.toDto(jobTypeChild);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JobTypeChildDto create(JobTypeChild resources) {
        return jobTypeChildMapper.toDto(jobTypeChildRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(JobTypeChild resources) {
        JobTypeChild jobTypeChild = jobTypeChildRepository.findById(resources.getId()).orElseGet(JobTypeChild::new);
        ValidationUtil.isNull( jobTypeChild.getId(),"JobTypeChild","id",resources.getId());
        jobTypeChild.copy(resources);
        jobTypeChildRepository.save(jobTypeChild);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            jobTypeChildRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<JobTypeChildDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (JobTypeChildDto jobTypeChild : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" pid",  jobTypeChild.getPid());
            map.put("职位名称", jobTypeChild.getName());
            map.put("职位描述", jobTypeChild.getDesc());
            map.put(" createBy",  jobTypeChild.getCreateBy());
            map.put(" updateBy",  jobTypeChild.getUpdateBy());
            map.put(" createTime",  jobTypeChild.getCreateTime());
            map.put(" updateTime",  jobTypeChild.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}